package org.example.ski_demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.ski_demo.entity.Track;
import org.example.ski_demo.service.TrackService;
import org.example.ski_demo.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/track")
public class TrackController {
    @Autowired
    private TrackService trackService;

    @GetMapping("/list")
    public List<Track> list() {
        return trackService.list();
    }

    @GetMapping("/load")
    public PageVO load(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam(value = "keyWord", required = false) String keyWord,
            @RequestParam(value = "type", required = false) String type
    ) {
        Page<Track> pageModel = new Page<>(page, size);
        QueryWrapper<Track> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(keyWord), type, keyWord);
        Page<Track> resultPage = this.trackService.page(pageModel, queryWrapper);
        return new PageVO(resultPage.getRecords(), resultPage.getSize(), resultPage.getTotal());
    }

    @PostMapping("/add")
    public Boolean add(@RequestBody Track track) {
        return trackService.save(track);
    }

    @PutMapping("/update")
    public Boolean update(@RequestBody Track track) {
        return trackService.updateById(track);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable("id") Integer id) {
        return this.trackService.removeById(id);
    }

}

